/*
 * @Description:
 * @Version: 1.0
 * @Autor: lzc
 * @Date: 2022-05-31 15:14:15
 * @LastEditors: lzc
 * @LastEditTime: 2022-08-12 15:11:44
 */
#ifndef _PAN_MAIN_H
#define _PAN_MAIN_H
#define STORAGE_NAMESPACE "storage" //数据库表名
#define NVS_APP_INFO "app_info"     //保存配网信息的字段名
#define NVS_VER_INFO "ver_info"     //保存配网信息的版本

#define PUBLISH_BUF 2048

#include "stdint.h"
#include "esp_err.h"
#define CHECK_ERROR_CODE(TAG, returned, expected) ({ \
  if (returned != expected)                          \
  {                                                  \
    ESP_LOGI(TAG, "TWDT ERROR\n\n");                 \
  }                                                  \
})

#define SYS_OK 0
#define SYS_ERR_NO_MEMORY 1
#define SYS_ERR_SET_SILENCE_FAILED 2
#define SYS_ERR_SET_TO_BLT_FAILED 3
#define SYS_ERR_ERASE_FLASH_FAILED 4
#define SYS_ERR_CONNECT_OTA_SERVER_FAILED 5
#define SYS_ERR_HTTPS_REQUEST_FAILED 6
#define SYS_ERR_FW_LENGTH_ILLEGAL 7
#define SYS_ERR_GET_HTTPS_HEAD_FAILED 8
#define SYS_ERR_GET_HTTPS_BODY_FAILED 9
#define SYS_ERR_WRITE_TARGET_MEMORY_FAILED 10
#define SYS_ERR_SEND_FIRMWARE_DATA_FAILED 11
#define SYS_ERR_READ_HTTPS_RESPONSE_FAILED 12
#define SYS_ERR_NO_FW_ON_OTA_SERVER 13
#define SYS_ERR_RECEIVED_FW_LENGTH_ILLEGAL 14
#define SYS_ERR_FW_CRC_FAILED 15
#define SYS_ERR_SET_TARGET_TO_APPLICATION_FAILED 16
#define SYS_ERR_HTTPS_OTA_BEGIN_FAILED 17
#define SYS_ERR_HTTPS_OTA_READ_IMG_DESC_FAILED 18
#define SYS_ERR_IMAGE_HEADER_VERIFICATION_FAILED 19
#define SYS_ERR_COMPLETE_DAT_WAS_NOT_RECEIVED 20
#define SYS_ERR_IMAGE_VALIDATION_FAILED 21

// typedef struct
// {
//   uint8_t  password[64];
//   uint8_t  ssid[32];
//  // uint8_t  bssid[32];
//   uint8_t  server_ip[64];
//   uint8_t  SerialNumber[12];
//   uint8_t  version[4];          //version[2].version[1].version[0](version[3]暂不用)
//   uint8_t  mattress_version[4];    //version[3].version[2].version[1].version[0]
//  // uint8_t  macAddr[6];          //macAddr[0]:macAddr[1]:macAddr[2]:macAddr[3]:macAddr[4]:macAddr[5]
//   uint16_t port;
// }APP_INFO;
typedef struct
{
  uint8_t password[64];
  uint8_t ssid[32];
  uint8_t server_ip[64];
  uint8_t SerialNumber[12];
  uint8_t version[4];          // version[2].version[1].version[0](version[3]暂不用)
  uint8_t mattress_version[4]; // version[3].version[2].version[1].version[0]
  uint16_t port;
} APP_INFO;

typedef struct
{
  uint8_t version[4];
  uint8_t mattress_version[4];
  uint32_t BinCurrentNo;
  char *url;

} FIRMWARE_INFO;

typedef struct
{
  char Request[256];
  char Server[100];
  char Port[10];
  char Data[512];
  char *URL;

} HttpInfo_t;

esp_err_t user_nvs_save_blob(const char *key, const void *value, size_t length);
esp_err_t user_nvs_read_blob(const char *key, void *out_value, size_t *length);

void wifi_init_sta(void *para);
int sendData(const char *data, const int len);
void uart_init(void);
void mqtt_app_start(void);
void wifi_init_softap(void);
void create_ap_server(void *para);
void app_http_task(void *para);
void SendToServerOTAbegin();
void SendToServerOTAover();

#endif
